package au.com.betaversion.service.fileload;

import java.net.UnknownHostException;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class MongoDbHelper {

	private int port = 10100;
	private String host = "127.0.0.1";
	private String database = "db";
	private String username = "015df313-d589-439c-a87b-9f0176f56891";
	private String password = "3ca9153d-70a2-48e5-9363-25d02343662b";
	
	private Mongo mongo = null;
	
	/**
	 * 
	 */
	public MongoDbHelper() {
		this.mongo = this.getConnection();
	}
	
	/**
	 * 	
	 * @param host
	 * @param port
	 * @param database
	 * @param username
	 * @param password
	 */
	public MongoDbHelper(String host, int port, String database,
			String username, String password) {
		super();
		this.port = port;
		this.host = host;
		this.database = database;
		this.username = username;
		this.password = password;
		this.mongo = this.getConnection();
	}
	
	/**
	 * 
	 * @return MongoDB instance connected to localhost:27017
	 */
	public Mongo getConnection() {
		if(this.mongo == null)
			try {
				this.mongo = new Mongo(getHost(), getPort());
			} catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (MongoException e) {
				e.printStackTrace();
			}
		return this.mongo;
	}
	
	/**
	 * 
	 * @param database
	 * @return
	 */
	public DB getDB() {
		return this.mongo.getDB(this.getDatabase());
	}
	
	/**
	 * 
	 * @param database
	 * @param username
	 * @param password
	 * @return
	 */
	public boolean authenticate() {
		return this.getDB().authenticate(this.getUsername(), this.getPassword().toCharArray());
	}

	/**
	 * 
	 * @param db
	 * @param collection
	 * @return
	 */
	public DBCollection getCollection(String collection) {
		return this.getDB().getCollection(collection);
	}

	/**
	 * @return the port
	 */
	public int getPort() {
		return port;
	}
	/**
	 * @param port the port to set
	 */
	public void setPort(int port) {
		this.port = port;
	}
	/**
	 * @return the host
	 */
	public String getHost() {
		return host;
	}
	/**
	 * @param host the host to set
	 */
	public void setHost(String host) {
		this.host = host;
	}
	/**
	 * @return the database
	 */
	public String getDatabase() {
		return database;
	}
	/**
	 * @param database the database to set
	 */
	public void setDatabase(String database) {
		this.database = database;
	}
	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}
	/**
	 * @param username the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}
	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}
	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}
	
}
